$(function() {
    $.initPage(eLancoSubmitList.load.start);
});

var eLancoSubmitList = new BaseFrame();

eLancoSubmitList.addBlock(
    'search',
    'list',
    'pagination'
);

//load module
eLancoSubmitList.load.addStart(function() {
    var params = eLancoSubmitList.data.search.params;
    $.initSubmitPage(params)
        .then(function(data) {
            _data = $.extend({}, data);
            return $.pageStart(_eLancoSettings);
        })
        .then(function() {
            eLancoSubmitList.load.runTask();
            eLancoSubmitList.event.runTask();
            eLancoSubmitList.load.list.create(_data.submitList, true);
        });
});

eLancoSubmitList.load.addBind('createTitle', function() {
    var currentPage = $.getCurrentPage(_eLancoSettings.pageList);
    $('title').text(currentPage.title);
});

eLancoSubmitList.load.addBind('createNav', function() {
    var navList = $.getUserAuthorityList(_user.roleList, _eLancoSettings.authorityList, _eLancoSettings.pageList);
    $('#logout').on('click', $.logoff);
    $('#block-nav').createNavList(navList).removeClass('dn');
});

eLancoSubmitList.load.search.addBind('create', function() {
    if (eLancoSubmitList.verify.isSubmitModel()) return;
    var domStorage = $('#form-search');
    $('#field-search-type').createSelect(domStorage, 'search.formId', $.getKeyValueList('id', 'formChineseName', _data.formList), {
        verify: false,
        firstOption: _allSelect,
    });
    $('#field-search-formNumber').createString(domStorage, 'search.formNumber', {
        verify: false,
    });
    $('#field-search-status').createSelect(domStorage, 'search.formStatus', $.getKeyValueListByObject(_enumFormSearchStatus), {
        verify: false,
        firstOption: _allSelect,
    });
    $('#field-search-activityNameDescription').createString(domStorage, 'detail.activityNameDescription', {
        verify: false,
    });
    $('#block-search .search-item[formId]').hide();
    $('#block-search').removeClass('dn');
});

eLancoSubmitList.load.list.addEvent('create', function(dataList, isFirst) {
    if (dataList.length == 0) {
        $('#block-alert').text(isFirst ? _enumMessage.noResult : _enumMessage.noSearchResult);
        $('#list-list').addClass('dn');
        $('#block-alert').removeClass('dn');
        $('#block-pagination').addClass('dn');
    } else {
        var formId = $('#form-search').data('search').formId;
        var headerTemplate = $('#template-list-header').html();
        var listTemplate = $('#template-list-detail').html();
        if (!$.isEmpty(formId)) {
            headerTemplate = $('#template-list-header-' + formId).html();
            listTemplate = $('#template-list-detail-' + formId).html();
        }
        $('#list-list thead').empty().createTemplate(headerTemplate);
        $('#list-list tbody').empty().createListTemplate(listTemplate, dataList);
        eLancoSubmitList.load.pagination.create(_data.pageTotal);
        $('#list-list').removeClass('dn');
        $('#block-alert').addClass('dn');
        $("[data-toggle='popover']").popover();
    }
    $('#block-list').removeClass('dn');
});

eLancoSubmitList.load.list.addEvent('update', function(pageNumber) {
    var params = eLancoSubmitList.save.search.params();
    params.pageNumber = pageNumber || params.pageNumber;
    $.getSubmitList(params)
        .then(function(data) {
            if (data.pageId != eLancoSubmitList.data.search.params.pageId) return false;
            _data.submitList = data.submitList;
            _data.pageTotal = data.pageTotal;
            eLancoSubmitList.load.list.create(_data.submitList);
        });
});

eLancoSubmitList.load.pagination.addEvent('create', function() {
    if (_data.submitList.length == 0) return;
    pageNumber = eLancoSubmitList.data.search.params.pageNumber;
    pageSize = eLancoSubmitList.data.search.params.pageSize;
    pageTotal = _data.pageTotal
    $('#block-pagination').createPagination({
        pageSize: pageSize,
        pageNumber: pageNumber,
        pageTotal: pageTotal,
    }).removeClass('dn');
});

//event module
eLancoSubmitList.event.search.addBind('formTypeChange', function() {
    $('#field-search-type').findFormUnit().on('change', function() {
        var thisDom = $(this);
        var value = thisDom.val();
        var formId = $('#form-search').data('search').formId;
        $('#form-search .search-item[formId]').fadeOut();
        if (!$.isEmpty(value)) {
            $('#form-search .search-item[formId=' + formId + ']').fadeIn();
        }
    });
});

eLancoSubmitList.event.search.addBind('operation', function() {
    $('#block-search').on('click', '#search-btn', function() {
        eLancoSubmitList.load.list.update(1);
        return false;
    });
});

eLancoSubmitList.event.list.addBind('operation', function() {
    $('#list-list').on('click', '.btn[delete]', function() {
        var thisDom = $(this);
        var data = thisDom.parents('tr[data]').data('data');
        $.confirm(eLancoSubmitList.message.list.deleteDetail, function() {
            $.delete(_apiPath + '/ELancos/FormDetails/' + data.id + '/Delete')
                .then(function(result) {
                    $.alert(_enumMessage.deleteSuccess, eLancoSubmitList.load.list.update);
                });
        });
    });

    $('#list-list').on('click', '.btn[cancel]', function() {
        var thisDom = $(this);
        var data = thisDom.parents('tr[data]').data('data');
        delete data.approvalList;
        $.confirm(eLancoSubmitList.message.list.cancelDetail, function() {
            $.post(_apiPath + '/ELancos/FormDetails/' + data.id + '/Cancel', data)
                .then(function(result) {
                    $.alert(_enumMessage.cancelSuccess, eLancoSubmitList.load.list.update);
                });
        });
    });
});

eLancoSubmitList.event.pagination.addBind('operation', function() {
    $('#block-pagination').on('changeCallback', function(event, currentPage, targetPage) {
        eLancoSubmitList.load.list.update(targetPage);
    });
});

//form module

//save module
eLancoSubmitList.save.search.addEvent('params', function(pageNumber) {
    var data = $('#form-search').data();
    var params = eLancoSubmitList.data.search.params;
    pageNumber = pageNumber || params.pageNumber;
    params.pageId++;
    params.pageNumber = pageNumber;
    params = $.extend({}, params, data.search);
    params.detail = $.extend({}, data.detail);
    eLancoSubmitList.data.search.params = params;
    return params;
});

//verify module
eLancoSubmitList.verify.addEvent('isSubmitModel', function() {
    if ($.getParam('model') == 'submit') return true;
    return false;
});

eLancoSubmitList.verify.addEvent('isApproveModel', function() {
    if ($.getParam('model') == 'approve') return true;
    return false;
});

//message module
eLancoSubmitList.message.list.addConfig('deleteDetail', '确定删除此申请？');

eLancoSubmitList.message.list.addConfig('cancelDetail', '确定撤回此申请？');

//data module
eLancoSubmitList.data.search.addConfig('params', {
    pageId: 0,
    pageNumber: 1,
    pageSize: 5,
});